Yacon.widget.TFile = function(element){
	if(element){
		this.element = element;
	}	
};

Yacon.widget.TFile.prototype = {
	
	/* 获取HTML信息 */	
	getHtml: function(){	
		
		var html = "";
		
		html += "<input ";
		
		html += " id='"+this.element.name+"' ";
		
		html += " class='file' name='"+this.element.name+"' type='file' ";
		
		if(this.element.isMultiple){
			html += " multiple ";
		}
		
		html += " field-name='"+this.element.name+"' ";
		
		html += " field-type='input' ";
		
		if(this.element.blank){
			html += " placeholder='"+this.element.blank+"' ";
		}
		
		if(!this.element.isView){
			html += " data-show-preview='false' ";
		}
		
		html += " />";
		
		
/*		if(this.element.innerComponent){
			html = this.parent.getComponentHtml(this.element.innerComponent,html);
		}*/
		
		return html;
	},
	
	addEvent: function(){
		
		var control = $("#"+this.element.name);
		
		//是否显示标题
		var showCaption = true;
		if(!this.element.isInput){
			showCaption = false;
		}
		
		var url = "/easyUpload";
		
		url += "?fileCharset="+this.element.fileCharset;
		url += "&fileLocation="+this.element.fileLocation;
		url += "&fileFill="+this.element.fileFill;
		url += "&fileFolder="+this.element.fileFolder;
		url += "&afterClass="+this.element.afterClass;
		
		var adapterUrl = getAbsolutePath(url);
		
		var config = {};		
		config.language = 'zh';                                    //设置语言
		config.uploadUrl = adapterUrl;                             //上传的地址
		config.allowedFileExtensions = this.element.fileTypes;     //接收的文件后缀
		config.showUpload = true;                                  //是否显示上传按钮
		config.showCaption = showCaption;                          //是否显示标题
		config.browseClass = "btn btn-primary";                    //按钮样式
		config.enctype = 'multipart/form-data';
		config.validateInitialCount = true;
		config.previewFileIcon = "<i class='glyphicon glyphicon-king'></i>";
		config.msgFilesTooMany = "选择上传的文件数量, 超过允许的最大数值{m}!";
		config.msgFilesTooLess = "选择上传的文件数量, 低于允许的最小数值{n}!";
		config.msgSizeTooLarge = "选择上传的文件{name}, 体积为 {size}, 超过最大体积{maxSize}!";
		config.msgSizeTooSmall = "选择上传的文件{name}, 体积为 {size}, 低于最小体积{minSize}!";
		config.msgInvalidFileExtension = "文件类型错误, 只允许上传 {extensions} 文件!";
		config.uploadAsync = this.element.uploadAsync;
		config.autoReplace = false;
		
		if(this.element.maxFileSize>0){
			config.maxFileSize = this.element.maxFileSize;           //单位为kb，如果为0表示不限制文件大小
		}
		
		if(this.element.minFileSize>0){
			config.minFileSize = this.element.minFileSize;           //单位为kb，如果为0表示不限制文件大小
		}
		
		if(this.element.minFileCount>0){
			config.minFileCount = this.element.minFileCount;
		}
		
		if(this.element.maxFileCount>0){
			config.maxFileCount = this.element.maxFileCount;
		}

		config.dropZoneEnabled = this.element.isZone;                          //是否显示拖拽区域
				
		//缩略图相关参数
		if(this.element.minImageWidth>0){
			config.minImageWidth = this.element.minImageWidth;
		}		
		if(this.element.maxImageWidth>0){
			config.maxImageWidth = this.element.maxImageWidth;
		}		
		if(this.element.minImageHeight>0){
			config.minImageHeight = this.element.minImageHeight;
		}		
		if(this.element.maxImageHeight>0){
			config.maxImageHeight = this.element.maxImageHeight;
		}
		
		//
		config.slugCallback = function(filename) {  
            return filename;  
        }  
		
		//处理完成
	    control.fileinput(config);
	    
	    var self = this;
	    
	    //文件选中处理 - numFiles:fileSize,label:fileName
	    $('#'+this.element.name).on('fileselect', function(event, numFiles, label) {
	    	
	    	if(self.element.maxFileSize == 1 && self.element.innerComponent){
	    		self.parent.value(self.element.innerComponent.name,label);
	    	}
	    	
	    	Yacon.callBack(self.element.name+"_select", [self.parent,self.parent.page,label,numFiles]);
	    });
	    
	    //批量操作
	    $('#'+this.element.name).on('filebatchselected', function(event, files) {
	    	
	    	//逐渐累加
	    	if(self.element.maxFileSize != 1 && self.element.innerComponent){
	    		
	    		var fileName = "";	    		
	    		for(var i=0;i<files.length;i++){
	    			fileName += ","+files[i].name;			
	    		}	    		
	    		if(fileName != ""){
	    			fileName = fileName.substring(1);
	    		}	    		
	    		self.parent.value(self.element.innerComponent.name,fileName);
	    		
	    	}
	    	
	    	Yacon.callBack(self.element.name+"_batchselect", [self.parent,self.parent.page,files]);
	    });
	    
	    //文件移除前操作
	    $('#'+this.element.name).on('filepreremove', function(event, id, index) {
	    	Yacon.callBack(self.element.name+"_remove", [self.parent,self.parent.page,index,id]);
	    });
	    
	    //文件移除后操作
	    $('#'+this.element.name).on('fileremoved', function(event, id, index) {
	    	
	    	//文件移除前操作
	    	var files = $("#"+self.element.name).fileinput("getFileStack");
	    	if(self.element.innerComponent){    		
	    		var fileName = "";	    		
	    		for(var i=0;i<files.length;i++){
	    			fileName += ","+files[i].name;			
	    		}	    		
	    		if(fileName != ""){
	    			fileName = fileName.substring(1);
	    		}	    		
	    		self.parent.value(self.element.innerComponent.name,fileName);
	    	}
	    		
	    	Yacon.callBack(self.element.name+"_removed", [self.parent,self.parent.page,index,id]);
	    });
	    
	    //文件清除操作
	    $('#'+this.element.name).on('filecleared', function(event) {
	    	
	    	if(self.element.innerComponent){
	    		self.parent.value("CAN_PIC_FILE","");
	    	}
	    	   	
	    	Yacon.callBack(self.element.name+"_clear", [self.parent,self.parent.page]);
	    });
	    
	    //异步成功处理
	    $("#"+this.element.name).on("fileuploaded", function (event, data, previewId, index) {	
	    	//alert(data.filenames);
		    Yacon.callBack(self.element.name+"_success", [self.parent,self.parent.page,data]);	    			    	 	
	    });
	    
	    //异步错误处理
	    $("#"+this.element.name).on('fileerror', function(event, data, msg) {
	    	Yacon.callBack(self.element.name+"_error", [self.parent,self.parent.page,data]);
	    });
	    
	    //同步步错误处理
	    $("#"+this.element.name).on('filebatchuploaderror', function(event, data, msg) {
	    	Yacon.callBack(self.element.name+"_error", [self.parent,self.parent.page,data]);
	    });
	    
	    //同步成功处理
	    $("#"+this.element.name).on("filebatchuploadsuccess", function (event, data, previewId, index) {	
		    Yacon.callBack(self.element.name+"_success", [self.parent,self.parent.page,data]);	    			    		    	
	    });
	    
	    //上传前处理
	    $("#"+this.element.name).on('filepreupload', function(event, data, previewId, index) {
	    	Yacon.callBack(self.element.name+"_before", [self.parent,self.parent.page,data]);	 
	    });
	    
	    //样式调整	    
	    if(this.element.innerComponent){
	    	$("#"+this.element.name).parent().parent().parent().prepend(this.getInnerHtml());
	    	$("#"+this.element.name).parent().parent().parent().find(".file-caption").hide();
	    }else{
	    	$("#"+this.element.name).parent().parent().parent().prepend(this.getLableHtml());
	    }
	    
	    

	},
	
	getLableHtml: function(){
				
		var html = "";
			
		if(this.element.isInput){
			
			html += "<span class='input-group-btn' >";
			
			html += new Yacon.widget.TButton(this.element.labelMenu).getFormHtml();
			
			html += "</span>";
			
		}
		
		return html;
	},
	
	getInnerHtml:function(){
		
		var html = "";
		
		if(this.element.innerComponent){
			
			var activeElement = new Yacon.widget.TInput(this.element.innerComponent);
			activeElement.parent = this.parent;
			
			this.parent.items.setFieldValue(this.element.innerComponent.name,activeElement);
			
			html += activeElement.getInnerHtml();
			
		}
		
		return html;
		
	}
		
}